<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="1024" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #currencySlot="{ model, field }">
        <a-input-search :disabled="isDetail" v-model:value="model[field]" placeholder="请选择交易币种名称" readonly @search="open(1)" />
      </template>
      <template #paymentConditionSlot="{ model, field }">
        <a-input-search :disabled="isDetail" v-model:value="model[field]" placeholder="请选择付款条件" readonly @search="open(2)" />
      </template>
      <template #accountConditionSlot="{ model, field }">
        <a-input-search v-model:value="model[field]" :disabled="isDetail" placeholder="请选择立账条件" readonly @search="openPaymentCondition(2)" />
      </template>
      <template #receivingPrincipleSlot="{ model, field }">
        <a-input-search v-model:value="model[field]" placeholder="请选择收货原则" :disabled="isDetail" @search="shippingPrincipleRegModalOpen" />
      </template>
    </BasicForm>
  </BasicModal>
  <!-- 表单区域 -->
  <currencySelectModel @register="regModal" @getSelectResult="handleSuccess" v-bind="getBindValue"></currencySelectModel>
  <!-- 表单区域 -->
  <paymentConditionSelectModel
    @register="regPaymentConditionModal"
    @getSelectResult="handlePaymentConditionSuccess"
    v-bind="getBindValue"
  ></paymentConditionSelectModel>
  <!-- 收货原则 -->
  <shippingPrincipleSelectModel
    @register="shippingPrincipleRegModal"
    @getSelectResult="shippingPrincipleandleSuccess"
    v-bind="getBindValue"
  ></shippingPrincipleSelectModel>
</template>

<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from '../BasSupplier.data';
import { saveOrUpdate } from '../BasSupplier.api';
import currencySelectModel from './currencySelectModel.vue';
import paymentConditionSelectModel from './paymentConditionSelectModel.vue';
import { localStorage } from '/@/utils/store/locaStore';
import shippingPrincipleSelectModel from '/@/views/components/shippingPrincipleSelectModel.vue';
import {  getTableList } from '/@/api/common/api';

// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
const isDetail = ref<boolean>(false);
//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 150,
  schemas: formSchema,
  showActionButtonGroup: false,
  baseColProps: { span: 8 },
});
function shippingPrincipleandleSuccess(val, values) {
  setFieldsValue({
    receivingPrincipleName: val[0].label,
    receivingPrincipleId: val[0].value,
  });
}
const [shippingPrincipleRegModal, { openModal: openShippingPrincipleRegModal }] = useModal();
//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //重置表单
  await resetFields();
  setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
  isDetail.value = !!data?.isDetail;
  if (unref(isUpdate)) {
    //表单赋值
    await setFieldsValue({
      ...data.record,
    });
  }
  // 隐藏底部时禁用整个表单
  setProps({ disabled: !data?.showFooter });
});
function shippingPrincipleRegModalOpen(props) {
  openShippingPrincipleRegModal(true,{
    isUpdate: false,
    showFooter: true,
  })
}
//设置标题
const title = computed(() => (unref(isDetail) ? '详情' : !unref(isUpdate) ? '新增' : '编辑'));
//表单提交事件
async function handleSubmit(v) {
  try {
    let values = await validate();
    setModalProps({ confirmLoading: true });
    if(localStorage.get('departId')) {
      values.departName = localStorage.get('departId')
      localStorage.remove('departId');
    }
    const res = await getTableList({pageNo:1,pageSize:9999})
    values.salesmanName = res.records.filter(item => {
      console.log(item.id === values.salesmanId);
      return item.id === values.salesmanId
    })[0]?.realname || ''
    //提交表单
    await saveOrUpdate(values, isUpdate.value);
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
const getBindValue = Object.assign({}, '');
//选择回写
function handleSuccess(val, values, type) {
  console.log('库管员',val);
  
  setFieldsValue({
    currencyName: val[0].label,
    currencyId: val[0].value,
  });
}
//选择条件回写
function handlePaymentConditionSuccess(val, values, type) {
  if (!type) {
    setFieldsValue({
      paymentConditionName: val[0].label,
      paymentConditionId: val[0].value,
    });
  } else {
    setFieldsValue({
      accountConditionName: val[0].label,
      accountConditionId: val[0].value,
    });
  }
}
const [regModal, { openModal }] = useModal();
const [regPaymentConditionModal, { openModal: openPaymentConditionModal }] = useModal();
function open(type) {
  if (type == 1) {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  } else if (type == 2) {
    openPaymentConditionModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
}
//打开选择条件
function openPaymentCondition(type) {
  openPaymentConditionModal(true, {
    isUpdate: false,
    showFooter: true,
    type: type,
  });
}
</script>

<style lang="less" scoped>
/** 时间和数字输入框样式 */
:deep(.ant-input-number) {
  width: 100%;
}

:deep(.ant-calendar-picker) {
  width: 100%;
}
</style>